﻿namespace DimensionsHelper.LanguageService.Syntax;

public abstract class MetadataSingleArgumentSuffix<T> : Declaration where T : Expression
{
    private T? _argument;

    internal MetadataSingleArgumentSuffix(SyntaxKinds kind, SyntaxToken declareKeywordToken, Identifier name,
        SyntaxNode? parent) :
        base(kind, declareKeywordToken, name, parent)
    {
    }

    /// <summary>
    ///     参数节点，允许空值。
    /// </summary>
    public T? Argument => _argument;


    public void Update(T argument)
    {
        SetChildNullableField(ref _argument, argument);
    }


    public override void AcceptVisitor<TV>(SyntaxNodeVisitor<TV> visitor)
    {
        // NameIdentifier 和 DeclareKeywordToken 是一样的，
        // 选其中一个遍历即可
        Accept(visitor, DeclareKeywordToken, Argument);
    }
}


public static partial class NodeFactory
{
    public static TNode UpdateMetadataSingleArgumentSuffix<TNode, TValue>(TNode node, TValue? value)
        where TValue : Expression
        where TNode : MetadataSingleArgumentSuffix<TValue>
    {
        if (value != null)
        {
            node.Update(value);
        }

        return node;
    }
}